package com.c2soft.ncmxSocket;

import java.io.Serializable;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.util.Random;

public class QuoteData implements Serializable {

	private static final long serialVersionUID = 1L;

	// private byte[] quoteData = new byte[82];

	private short exchID_;

	private int tokenID;
	
	private String symbol;

	private int bid1;

	private int bidQ1;

	private int ask1;

	private int askQ1;

	private int ltp;

	private int ltq;

	private int ltt;

	private int open;

	private int high;

	private int low;

	private int close;

	private double tbq;

	private double tsq;

	private double vol;

	private double openInt;
	


	public short getExchID_() {
		return exchID_;
	}

	public QuoteData(short exchID_,int  tokenID,String symbol, int bid1, int bidQ1, int ask1,
			int askQ1, int ltp, int ltq, int ltt, int open, int high, int low,
			int close, double tbq, double tsq, double vol, double openInt) {

		this.exchID_ = exchID_;
		this.tokenID=tokenID;
		this.symbol = symbol;
		this.bid1 = bid1;
		this.bidQ1 = bidQ1;
		this.ask1 = ask1;
		this.askQ1 = askQ1;
		this.ltp = ltp;
		this.ltq = ltq;
		this.ltt = ltt;
		this.open = open;
		this.high = high;
		this.low = low;
		this.close = close;
		this.tbq = tbq;
		this.tsq = tsq;
		this.vol = vol;
		this.openInt = openInt;
	}

	public void setExchID_(short exchID_) {
		this.exchID_ = exchID_;
	}

	
	public int getBid1() {
		return bid1;
	}

	public void setBid1(int bid1) {
		this.bid1 = bid1;
	}

	public int getBidQ1() {
		return bidQ1;
	}

	public void setBidQ1(int bidQ1) {
		this.bidQ1 = bidQ1;
	}

	public int getAsk1() {
		return ask1;
	}

	public void setAsk1(int ask1) {
		this.ask1 = ask1;
	}

	public int getAskQ1() {
		return askQ1;
	}

	public void setAskQ1(int askQ1) {
		this.askQ1 = askQ1;
	}

	public int getLtp() {
		return ltp;
	}

	public void setLtp(int ltp) {
		this.ltp = ltp;
	}

	public int getLtq() {
		return ltq;
	}

	public void setLtq(int ltq) {
		this.ltq = ltq;
	}

	public int getLtt() {
		return ltt;
	}

	public void setLtt(int ltt) {
		this.ltt = ltt;
	}

	public int getOpen() {
		return open;
	}

	public void setOpen(int open) {
		this.open = open;
	}

	public int getHigh() {
		return high;
	}

	public void setHigh(int high) {
		this.high = high;
	}

	public int getLow() {
		return low;
	}

	public void setLow(int low) {
		this.low = low;
	}

	public int getClose() {
		return close;
	}

	public void setClose(int close) {
		this.close = close;
	}

	public double getTbq() {
		return tbq;
	}

	public void setTbq(double tbq) {
		this.tbq = tbq;
	}

	public double getTsq() {
		return tsq;
	}

	public void setTsq(double tsq) {
		this.tsq = tsq;
	}

	public double getVol() {
		return vol;
	}

	public void setVol(double vol) {
		this.vol = vol;
	}

	public double getOpenInt() {
		return openInt;
	}

	public void setOpenInt(double openInt) {
		this.openInt = openInt;
	}

	public byte[] toByteArray() {
		
		byte[] exchID_byteArr_1 = shortToByteArray(exchID_);
		byte[] tokenID_byteArr_2 = intToByteArray(tokenID);
		byte[] bid1_byteArr_3 = intToByteArray(bid1);
		byte[] bidQ1_byteArr_4 = intToByteArray(bidQ1);
		byte[] ask1_byteArr_5 = intToByteArray(ask1);
		byte[] askQ1_byteArr_6 = intToByteArray(askQ1);
		byte[] ltp_byteArr_7 = intToByteArray(ltp);
		byte[] ltq_byteArr_8 = intToByteArray(ltq);
		byte[] ltt_byteArr_9 = intToByteArray(ltt);
		byte[] open_byteArr_10 = intToByteArray(open);
		byte[] high_byteArr_11 = intToByteArray(high);
		byte[] low_byteArr_12 = intToByteArray(low);
		byte[] close_byteArr_13 = intToByteArray(close);
		byte[] tbq_byteArr_14 = doubleToByteArray(tbq);
		byte[] tsq_byteArr_15 = doubleToByteArray(tsq);
		byte[] vol_byteArr_16 = doubleToByteArray(vol);
		byte[] openInt_byteArr_17 = doubleToByteArray(openInt);

		return concat(exchID_byteArr_1, tokenID_byteArr_2, bid1_byteArr_3,
				bidQ1_byteArr_4, ask1_byteArr_5, askQ1_byteArr_6,
				ltp_byteArr_7, ltq_byteArr_8, ltt_byteArr_9, open_byteArr_10,
				high_byteArr_11, low_byteArr_12, close_byteArr_13,
				tbq_byteArr_14, tsq_byteArr_15, vol_byteArr_16,
				openInt_byteArr_17);
	}

	public byte[] shortToByteArray(short x) {
		return ByteBuffer.allocate(2).order(ByteOrder.LITTLE_ENDIAN)
				.putShort(x).array();
	}

	public byte[] intToByteArray(int x) {
		return ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(x)
				.array();
	}

	public byte[] doubleToByteArray(double x) {
		return ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN)
				.putDouble(x).array();
	}

	
	/*
	 * private void appendArray(byte[] component, int endPosition) {
	 * System.arraycopy(component, 0, quoteData, endPosition, component.length);
	 * }
	 */

	private static byte[] concat(byte[]... arrays) {
		int lengh = 0;
		for (byte[] array : arrays) {
			lengh += array.length;
		}
		byte[] result = new byte[lengh];
		int pos = 0;
		for (byte[] array : arrays) {
			for (byte element : array) {
				result[pos] = element;
				pos++;
			}
		}
		return result;
	}

	@Override
	public String toString() {
		return "QuoteData [exchID_=" + exchID_ + ", tokenID=" + tokenID
				+ ", symbol=" + symbol + ", bid1=" + bid1 + ", bidQ1=" + bidQ1
				+ ", ask1=" + ask1 + ", askQ1=" + askQ1 + ", ltp=" + ltp
				+ ", ltq=" + ltq + ", ltt=" + ltt + ", open=" + open
				+ ", high=" + high + ", low=" + low + ", close=" + close
				+ ", tbq=" + tbq + ", tsq=" + tsq + ", vol=" + vol
				+ ", openInt=" + openInt + "]";
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ask1;
		result = prime * result + askQ1;
		result = prime * result + bid1;
		result = prime * result + bidQ1;
		result = prime * result + close;
		result = prime * result + high;
		result = prime * result + low;
		result = prime * result + ltp;
		result = prime * result + ltq;
		result = prime * result + ltt;
		result = prime * result + open;
		long temp;
		temp = Double.doubleToLongBits(openInt);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		result = prime * result + ((symbol == null) ? 0 : symbol.hashCode());
		temp = Double.doubleToLongBits(tbq);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(tsq);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		temp = Double.doubleToLongBits(vol);
		result = prime * result + (int) (temp ^ (temp >>> 32));
		return result;
	}

	
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		QuoteData other = (QuoteData) obj;
		if (ask1 != other.ask1)
			return false;
		if (askQ1 != other.askQ1)
			return false;
		if (bid1 != other.bid1)
			return false;
		if (bidQ1 != other.bidQ1)
			return false;
		if (close != other.close)
			return false;
		if (high != other.high)
			return false;
		if (low != other.low)
			return false;
		if (ltp != other.ltp)
			return false;
		if (ltq != other.ltq)
			return false;
		if (ltt != other.ltt)
			return false;
		if (open != other.open)
			return false;
		if (Double.doubleToLongBits(openInt) != Double
				.doubleToLongBits(other.openInt))
			return false;
		if (symbol == null) {
			if (other.symbol != null)
				return false;
		} else if (!symbol.equals(other.symbol))
			return false;
		if (Double.doubleToLongBits(tbq) != Double.doubleToLongBits(other.tbq))
			return false;
		if (Double.doubleToLongBits(tsq) != Double.doubleToLongBits(other.tsq))
			return false;
		if (Double.doubleToLongBits(vol) != Double.doubleToLongBits(other.vol))
			return false;
		return true;
	}

	
}
